/******************************************************************************
 * @File name   :      MemberGiftServiceImpl.java
 *
 * @Author      :      GUDONG
 *
 * @Date        :      2015年8月25日
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2015年8月25日 下午5:51:41        GUDONG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.mars.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.commonfunc.common.util.UserUtil;
import com.cap.bts.crmdp.dealer.activation.domain.PackageRedemptionLines;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.mars.service.IMarsActivationService;
import com.cap.bts.crmdp.dealer.mars.service.IMarsMaintainMemberService;
import com.cap.bts.crmdp.dealer.mars.service.IMarsMemberGiftService;
import com.cap.bts.crmdp.dealer.mars.service.IMarsPurchasePackageService;
import com.cap.bts.crmdp.dealer.member.dto.MemberQueryDto;
import com.cap.bts.crmdp.dealer.member.dto.MemberResultsDto;
import com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao;
import com.cap.bts.crmdp.dealer.packagegift.domain.MemberGiftInfo;
import com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto;
import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.crmdp.dealer.purchase.domain.PackageDefinition;
import com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class MarsMemberGiftServiceImpl implements IMarsMemberGiftService {
    @Resource(name = "memberGiftDao")
    private IMemberGiftDao          memberGiftDao;

    @Autowired
    private IMarsActivationService activationService;
    
    @Autowired
    ITicketLineDefineDao iTicketLineDefineDao;
    
    @Autowired
    private IMarsMaintainMemberService maintainMemberService;

    @Autowired
    private IMarsPurchasePackageService purchasePackageService;

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getGiftByPackageDefId(java.lang.Long)
     **/
    @Override
    public List<MemberGiftInfo> getPackageGiftByPackageDefId(Long packageDefId) throws AppException {
        return memberGiftDao.getPackageGiftByPackageDefId(packageDefId);
    }
    
    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getTicketGiftByPackageDefId(java.lang.Long)
     **/
    @Override
    public List<MemberGiftInfo> getTicketGiftByPackageDefId(Long packageDefId) throws AppException {
        return memberGiftDao.getTicketGiftByPackageDefId(packageDefId);
    }
    

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#isFirstActivation(java.lang.Long,
     *      java.lang.Long)
     **/
    @Override
    public Boolean isFirstActivation(Long dealerId, Long memberId) throws AppException {
        List<MemberPackageInfo> list = memberGiftDao.getPackageInfoByMemberIdAndDealerId(dealerId,
            memberId);
        if (list != null && !list.isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月26日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#processPackageGiftAndPurchaseActivation(MemberPackageInfoSubmitDto,
     *      java.util.List)
     **/
    @Override
    public List<PackageDefinition> processPackageGiftAndPurchaseActivation(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto,
                                                                    List<MemberGiftInfo> memberGiftInfo)
                                                                                                        throws AppException {
        List<PackageDefinition> rtnList = new ArrayList<PackageDefinition>();
        for (int i = 0; i < memberGiftInfo.size(); i++) {
            List<PackageDefinition> list = memberGiftDao
                .getMemberPackageInfoListOfGiftByGiftId(memberGiftInfo.get(i).getGiftId());
            if (list != null && !list.isEmpty()) {
                rtnList.add(list.get(0));
                PackageDefinition packageDefinition = list.get(0);
                memberPackageInfoSubmitDto.setIsPhysicalCard((byte) 0);
                memberPackageInfoSubmitDto.setPackageDefId(packageDefinition.getId());
                memberPackageInfoSubmitDto.setPrice(packageDefinition.getPrice());
                memberPackageInfoSubmitDto.setGiftPurchase(true);
                MemberQueryDto queryDto = new MemberQueryDto();
                if(UserUtil.getCurrentUser()!=null){
                    queryDto.setDealerId(UserUtil.getCurrentUser().getDealerId());
                }else{
                    queryDto.setDealerId(memberPackageInfoSubmitDto.getDealerId());
                }
                queryDto.setId(memberPackageInfoSubmitDto.getMemberId());
                List<MemberResultsDto> result = maintainMemberService
                    .getMemberByMobilePhoneOrMemberId(queryDto);
                if (result != null && !result.isEmpty()) {
                    memberPackageInfoSubmitDto.setMemberName(result.get(0).getMemberName());
                    memberPackageInfoSubmitDto.setMobilePhone(result.get(0).getMobilePhone());
                }
                purchasePackageService.purchaseGiftPackageMars(memberPackageInfoSubmitDto);
            }
        }
        return rtnList;
    }
    
    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月26日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#processTicketGiftAndPurchaseActivation(MemberPackageInfoSubmitDto,
     *      java.util.List)
     **/
    @Override
    public List<Ticket> processTicketGiftAndPurchaseActivation(
        MemberPackageInfoSubmitDto memberPackageInfoSubmitDto, List<MemberGiftInfo> memberGiftInfo)
        throws AppException { 
        List<Ticket> rtnList = new ArrayList<Ticket>();
        if(memberGiftInfo==null){
            rtnList=null;
            return rtnList;
        }
       
        for (int i = 0; i < memberGiftInfo.size(); i++) {
            List<Ticket> list = memberGiftDao
                .getTicketListOfGiftByGiftId(memberGiftInfo.get(i).getGiftId());
            if (list != null && !list.isEmpty()) {
                rtnList.add(list.get(0));
            }
        }
        List<PackageRedemptionLines>packageRedemptionLinesList= new ArrayList<PackageRedemptionLines>();
        for (Ticket t : rtnList) {
            PackageRedemptionLines prLines=iTicketLineDefineDao.getPackageRedemptionLinesByTicketDefId(t.getId());
            if(prLines!=null){
                packageRedemptionLinesList.add(prLines);
            }
        }
        activationService.addPackageRedemptionListInfoMars(packageRedemptionLinesList, memberPackageInfoSubmitDto.getPackageId());
        return rtnList;
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月26日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#processGift(com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo,
     *      java.lang.Long, java.util.List)
     **/
    @Override
    public List<PackageDefinition> processPackageGift(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto)
        throws AppException {
        List<MemberGiftInfo> gift = this.getPackageGiftByPackageDefId(memberPackageInfoSubmitDto
            .getPackageDefId());
        if (gift != null
            && !gift.isEmpty()
            && this.isFirstActivation(memberPackageInfoSubmitDto.getDealerId(),
                memberPackageInfoSubmitDto.getMemberId())) {
             return this.processPackageGiftAndPurchaseActivation(
                memberPackageInfoSubmitDto, gift);
        }
        return new ArrayList<PackageDefinition>();
    }
    
    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : 2015年8月26日
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberTicketGiftService#processTicketGift(com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo,
     *      java.lang.Long, java.util.List)
     **/
    @Override
    public List<Ticket> processTicketGift(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto)
                                                                                                     throws AppException {
        List<MemberGiftInfo> gift = this.getTicketGiftByPackageDefId(memberPackageInfoSubmitDto
            .getPackageDefId());
        if (gift != null
            && !gift.isEmpty()
            && this.isFirstActivation(memberPackageInfoSubmitDto.getDealerId(),
                memberPackageInfoSubmitDto.getMemberId())) {
            return this.processTicketGiftAndPurchaseActivation(
                memberPackageInfoSubmitDto, gift);
        }
        return new ArrayList<Ticket>();
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Sep 23, 2015
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getGiftListInfo(com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto)
     **/
    @Override
    public List<PackageDefinition> getPackageGiftListInfo(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto)
                                                                                                         throws AppException {
        List<MemberGiftInfo> gift = this.getPackageGiftByPackageDefId(memberPackageInfoSubmitDto
            .getPackageDefId());
        if (gift != null
            && !gift.isEmpty()
            && this.isFirstActivation(memberPackageInfoSubmitDto.getDealerId(),
                memberPackageInfoSubmitDto.getMemberId())) {
            return this.getPackageGiftListDetailInfo(
                memberPackageInfoSubmitDto, gift);
        }
        return new ArrayList<PackageDefinition>();
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Sep 23, 2015
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getGiftListInfo(com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto)
     **/
    @Override
    public List<Ticket> getTicketGiftListInfo(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto)
                                                                                                    throws AppException {
        List<MemberGiftInfo> gift = this.getTicketGiftByPackageDefId(memberPackageInfoSubmitDto
            .getPackageDefId());
        if (gift != null
            && !gift.isEmpty()
            && this.isFirstActivation(memberPackageInfoSubmitDto.getDealerId(),
                memberPackageInfoSubmitDto.getMemberId())) {
            return this
                .getTicketGiftListDetailInfo(memberPackageInfoSubmitDto, gift);
        }
        return new ArrayList<Ticket>();
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Sep 23, 2015
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getGiftListDetailInfo(com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto,
     *      java.util.List)
     **/
    @Override
    public List<PackageDefinition> getPackageGiftListDetailInfo(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto,
                                                         List<MemberGiftInfo> memberGiftInfo)
                                                                                             throws AppException {

        List<PackageDefinition> rtnList = new ArrayList<PackageDefinition>();
        for (int i = 0; i < memberGiftInfo.size(); i++) {
            List<PackageDefinition> list = memberGiftDao
                .getMemberPackageInfoListOfGiftByGiftId(memberGiftInfo.get(i).getGiftId());
            if (list != null && !list.isEmpty()) {
                rtnList.add(list.get(0));
            }
        }
        return rtnList;
    }

    /**
     * 根据套餐ID、gift_type、gift_id查询入会礼信息
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getMermberGiftInfo(com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto)
     */
    @Override
    public MemberGiftInfo getMermberGiftInfo(MermberGiftDto mermberGiftDto) throws AppException {
        MemberGiftInfo memberGiftInfo = memberGiftDao.getMermberGiftInfo(mermberGiftDto);
        if (memberGiftInfo != null) {
            return memberGiftInfo;
        }
        return memberGiftInfo;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 24, 2015
     * @see com.cap.bts.crmdp.dealer.packagegift.service.IMemberGiftService#getTicketGiftListDetailInfo(com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto, java.util.List)
    **/
    @Override
    public List<Ticket> getTicketGiftListDetailInfo(MemberPackageInfoSubmitDto memberPackageInfoSubmitDto,
                                                    List<MemberGiftInfo> memberGiftInfo)throws AppException {
        List<Ticket> rtnList = new ArrayList<Ticket>();                                                                            
        if(memberGiftInfo==null){
            rtnList=null;
            return rtnList;
        }
       
        for (int i = 0; i < memberGiftInfo.size(); i++) {
            List<Ticket> list = memberGiftDao.getTicketListOfGiftByGiftId(memberGiftInfo.get(i)
                .getGiftId());
            if (list != null && !list.isEmpty()) {
                rtnList.add(list.get(0));
            }
        }
        return rtnList;
    }

    @Override
    public List<MemberGiftInfo> getGiftByPackageDefId(Long packageDefId) throws AppException {
        return memberGiftDao.getGiftByPackageDefId(packageDefId);
        	
    }

}
